package com.jingbian.cppccbackend.service.impl.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jingbian.cppccbackend.entity.Admins;
import com.jingbian.cppccbackend.entity.Department;
import com.jingbian.cppccbackend.mapper.admin.SuperMapper;
import com.jingbian.cppccbackend.service.admin.SuperService;
import com.jingbian.cppccbackend.entity.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author yezi
 * @since 2025-02-09
 */
@Service
public class SuperServiceImpl extends ServiceImpl<SuperMapper, Department> implements SuperService {

    @Autowired
    private SuperMapper superMapper;

    @Override
    public List<Department> getProList() {
        return superMapper.getProList();
    }


    @Override
    public List<Department> getPubList() {
        return superMapper.getPubList();
    }

    @Override
    public void addProDepartment(Department department) {
        superMapper.addProDepartment(department);
    }

    @Override
    public void addPubDepartment(Department department) {
        superMapper.addPubDepartment(department);
    }

    @Override
    public void deleteProDepartment(Integer id) {
        superMapper.deleteProDepartment(id);
    }

    @Override
    public void deletePubDepartment(Integer id) {
        superMapper.deletePubDepartment(id);
    }

    @Override
    public void updatePassword(Integer aid, String newPassword) {
        // 对密码进行加密
//        String encryptedPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        superMapper.updatePassword(aid, newPassword);
    }

    @Override
    public PageVO<Admins> getAdmins(Integer pageNum, Integer pageSize, String sortType, String permissionType, String statusType, String searchText) {
        PageVO<Admins> pb = new PageVO<>();
        PageHelper.startPage(pageNum, pageSize);

        List<Admins> admins = superMapper.getAdmins(
                sortType, permissionType, statusType, searchText);
        Page<Admins> pag = (Page<Admins>) admins;
        pb.setTotal(pag.getTotal());
        pb.setItems(pag.getResult());
        return pb;
    }

    @Override
    public void disableAdmin(Integer aid) {
        superMapper.disableAdmin(aid);
    }

    @Override
    public Admins getAdminById(Integer aid) {
        return superMapper.getAdminById(aid);
    }

    @Override
    public void updateAdmin(Admins admin) {
        // // 如果有密码更新，进行加密
        // if (admin.getPassword() != null && !admin.getPassword().isEmpty()) {
        //     // 对密码进行加密
        //     String encryptedPassword = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        //     admin.setPassword(encryptedPassword);
        // }

        // 更新管理员信息
        superMapper.updateAdmin(admin);
    }

    @Override
    public void addAdmin(Admins admin) {
        // // 对密码进行加密
        // String encryptedPassword = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        // admin.setPassword(encryptedPassword);
        superMapper.addAdmin(admin);
    }

    @Override
    public List<Admins> getAdminsForExport(String sortType, String permissionType, String statusType, String searchText) {
        // 直接调用 mapper 的 getAdmins 方法获取所有数据（不分页）
        return superMapper.getAdmins(sortType, permissionType, statusType, searchText);
    }

    @Override
    public boolean checkAccountExists(String account) {
        return superMapper.checkAccountExists(account) > 0;
    }
}